package cn.wolfcode.luowowo.controller;

import cn.wolfcode.luowowo.annotation.RequireLogin;
import cn.wolfcode.luowowo.annotation.UserParam;
import cn.wolfcode.luowowo.cache.service.IUserInfoRedisService;
import cn.wolfcode.luowowo.domain.Destination;
import cn.wolfcode.luowowo.domain.Travel;
import cn.wolfcode.luowowo.domain.TravelComment;
import cn.wolfcode.luowowo.domain.UserInfo;
import cn.wolfcode.luowowo.query.TravelCommentQuery;
import cn.wolfcode.luowowo.query.TravelQuery;
import cn.wolfcode.luowowo.service.IDestinationService;
import cn.wolfcode.luowowo.service.ITravelCommentService;
import cn.wolfcode.luowowo.service.ITravelService;
import cn.wolfcode.luowowo.util.JsonResult;
import cn.wolfcode.luowowo.util.UMEditorUploader;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

// 游记
@RestController
@RequestMapping("travels")
public class TravelController {

    @Autowired
    private ITravelService travelService;

    @Autowired
    private IDestinationService destinationService;

    @Autowired
    private IUserInfoRedisService userInfoRedisService;
    @Autowired
    private ITravelCommentService travelCommentService;

    // 目的地页面:游记分页
    @GetMapping("/query")
    public Object query(TravelQuery qo){
        return JsonResult.success(travelService.query(qo));
    }

    // 旅游日记页面:旅游地点下拉框
    @GetMapping("/input")
    public Object input(String id){

        // 查询旅游地点
        List<Destination> dests = destinationService.listAll();
        Map<String,Object> map = new HashMap<>();

        // 编辑时回显(前端页面设计有点不好,接口原则:一个接口一个功能)
        // ①查询目的地:destinations/list②数据回显: travel/detail?id=
        if(StringUtils.hasLength(id)){
            Travel tv = travelService.get(id);
            map.put("tv",tv);
        }
        map.put("dests",dests);
        return JsonResult.success(map);
    }

    // 旅游日记页面:百度富文本编辑器中(上传图片)
    @RequestMapping("/contentImage")
    public String uploadUEImage(MultipartFile upfile, HttpServletRequest request) throws Exception{
        UMEditorUploader up = new UMEditorUploader(request);
        String[] fileType = {".gif" , ".png" , ".jpg" , ".jpeg" , ".bmp"};
        up.setAllowFiles(fileType);
        up.setMaxSize(10000); //单位KB
        up.upload(upfile);

        String callback = request.getParameter("callback");
        String result = "{\"name\":\""+ up.getFileName() +"\", \"originalName\": \""+ up.getOriginalName() +"\", \"size\": "+ up.getSize()
                +", \"state\": \""+ up.getState() +"\", \"type\": \""+ up.getType() +"\", \"url\": \""+ up.getUrl() +"\"}";
        result = result.replaceAll( "\\\\", "\\\\" );
        if(callback == null ){
            return result ;
        }else{
            return "<script>"+ callback +"(" + result + ")</script>";
        }
    }

    // 旅游写日记页面:发表按钮
    @PostMapping("/saveOrUpdate")
    @RequireLogin// 获取当前用户Id,必须登录
    public Object saveOrUpdate(Travel travel, HttpServletRequest request){

        // 通过请求对象获取token
        String token = request.getHeader("token");
        // 通过token获取当前登录对象
        UserInfo user = userInfoRedisService.getUserByToken(token);
        // 把当前用不Id设置到游记对象中
        travel.setUserId(user.getId());

        travelService.saveOrUpdate(travel);
        // 页面需要返回的是新增/编辑游记
        return JsonResult.success(travel.getId());
    }

    // 使用某种方法简化获取当前对象(获取当前登录用户信息:简化效果如下---用户对象注入)
    // 在接口形式参数中声明用户对象,spingmvc自动将当前登录用户对象注入进来
    // 使用spingMvc参数解析器(注解区分请求参数)
    @GetMapping("/getUser")//自定义参数解析器获取当前登录对象
    public Object getUser(@UserParam UserInfo userInfo){

        // 1.获取当前登录用户对象
        return JsonResult.success(userInfo);
    }

    // 写游记页面:游记
    @GetMapping("/detail")
    public Object detail(String id){
        return JsonResult.success(travelService.get(id));
    }

    // 如果需要做用户编辑
    @GetMapping("/updateUser")
    public Object updateUser(UserInfo userInfo){
        // 获取当前登录用户对象
        return JsonResult.success(userInfo);
    }

    // 游记评论
    @RequireLogin
    @PostMapping("/commentAdd")
    public Object commentAdd(TravelComment comment,@UserParam UserInfo userInfo){

        BeanUtils.copyProperties(userInfo,comment);
        comment.setUserId(userInfo.getId());
        travelCommentService.save(comment);
        return JsonResult.success();
    }

    // 把游记评论查询出来
    @GetMapping("/comments")
    public Object comments(String travelId){
        // 获取当前登录用户对象
        return JsonResult.success(travelCommentService.queryByTravelId(travelId));
    }
}
